package com.holdtime.jtt809dataservice.client.codec;

import com.holdtime.jtt809dataservice.client.handler.SubordinatePlatformServerHandler;
import com.holdtime.jtt809dataservice.common.codec.JTT809Decoder;
import com.holdtime.jtt809dataservice.common.codec.JTT809Encoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * author: sunwei
 * Date: 2025/3/18 12:06
 * Description:
 */
@Component
public class ClientJtt809ProtocolInitalizer extends ChannelInitializer<SocketChannel> {
    @Autowired
    private SubordinatePlatformServerHandler subordinatePlatformServerHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //心跳检测,超时时间300秒，指定时间中没有读写操作会触发IdleStateEvent事件
        pipeline.addLast(new IdleStateHandler(0, 0, 300, TimeUnit.SECONDS));
        pipeline.addLast("decoder", new JTT809Decoder());
        pipeline.addLast("handler", subordinatePlatformServerHandler);
        pipeline.addLast("encoder", new JTT809Encoder());
    }
}
